package Majisuka
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.net.URLRequest;
	
	import mx.controls.Image;
	import mx.core.UIComponent;
	
	import spark.components.BorderContainer;

	public class Fighter extends People
	{
		private var _x:int ;
		private var _y:int ;
		private var _direct:int ; // direction 1-up, 0-down
		private var _graphData:BitmapData ;
		private var _graph:Bitmap ;
		
		public function Fighter()
		{
			super();
		}
		
		public function get graph():Bitmap
		{
			return _graph;
		}

		public function set graph(value:Bitmap):void
		{
			_graph = value;
		}

		public function get direct():int
		{
			return _direct;
		}

		public function set direct(value:int):void
		{
			_direct = value;
		}

		public function get y():int
		{
			return _y;
		}

		public function set y(value:int):void
		{
			_y = value;
		}

		public function get x():int
		{
			return _x;
		}

		public function set x(value:int):void
		{
			_x = value;
		}
		
		public function GetInfo( p:People ):void
		{
			var loader:Loader = new Loader() ;			
			
			_id = p.id ;
			_hp = p.hp ;
			_icon = p.icon ;
			_currHp = p.currHp ;
			_att = p.att ;
			_def = p.def ;
			_group = p.group ;
				
			loader.contentLoaderInfo.addEventListener( Event.COMPLETE, OnIconLoadComplete ) ;
			loader.load( new URLRequest( "icon\\" + _icon ) );
		}
		
		public function OnIconLoadComplete( evt:Event ):void
		{
			_graph = new Bitmap() ;
			_graphData = evt.target.content.bitmapData ;	
			_graph.bitmapData = _graphData ;
		}

		public function Draw( size:int ):void
		{			
			//var ui:UIComponent = new UIComponent() ;
			if ( _graph == null )
				return ;
			
			_graph.x = _x ;
			_graph.y = _y ;
			_graph.width = size ;
			_graph.height = size ;
			_graph.alpha = _currHp / _hp ;
			//field.addChild( _graph ) ;
			//field.contentGroup.addChild( _graph ) ;
			//field.addElement( ui ) ;
		}
		
		
		public function Injure( hit:int ):void
		{
			_currHp -= hit ;
			if ( _currHp < 0 )
				_currHp = 0 ;
		}
		
		public function Attack( enemy:Fighter, fromBack:Boolean ):void
		{
			// The fighter attacks enemy
			var hit:int ;
			hit = _att - enemy.def ;
			if ( hit <= 0 )
				hit = 1 ;
			if ( fromBack )
				hit *= 2 ;
			enemy.Injure( hit ) ;	
		}
		
		public function Move( dist:int, size:int, field:BorderContainer ):Boolean
		{
			// return true if hit the boundary.
			if ( _direct == 1 )
				_y -= dist ;
			else
				_y += dist ;
			
			if ( _y <= 0 ) 
			{
				_direct = 1 - _direct ;
				_y = 0 ;
				return true ;	
			}
			else if  ( y >= field.height - size )
			{
				_direct = 1 - _direct ;
				_y = field.height - size ;
				return true ;
			}
			
			return false ;
		}
		
		public function Overlap( f:Fighter, size:int ):Boolean
		{
			// Whether it overlaps with another fighter
			if ( _y - f.y > -size && _y - f.y < size 
				&& _x - f.x > -size && _x - f.x < size )
				return true ;
			return false ;
		}
	}
}